'''
 * CaRComp Execution Environment
 * 
 * Copyright (C) 2011 Taylor L. Riche
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
'''

import re

class Message(object):
    
    def __init__(self, body=''):
        self.__body__ = body
        if body != '':
            self.parse_msg()
            
    def parse_msg(self, body=''):
        if body != '':
            self.__body__ = body
        m = re.search('(\S+):([0-9]+):(\S+\s*)', self.__body__)
        self._node_name = m.group(1)
        self.time_stamp = m.group(2)
        self.content = m.group(3)
        return (self._node_name, self.content)

    def create_msg_body(self, node_name, time_stamp, content):
        self._node_name = node_name
        self.content = content
        self.time_stamp = int(time_stamp)
        self.__body__ = '{}:{}:{}'.format(node_name, int(time_stamp), content)
        
    def get_body(self):
        return self.__body__